/**
 * @Author:Geln Yang
 * @Created:2008-7-28 $ Revision: 1.0 $ $ Author: Geln Yang $ $ Date: 2008-7-28 $
 */
package com.hgy.commonsale.webui.action.user;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.geln.web.Constants;
import org.geln.web.context.ISessionContext;
import org.geln.web.util.URLUtils;
import com.hgy.commonsale.service.IUserManager;
import com.hgy.commonsale.user.domain.User;
import com.hgy.commonsale.webui.action.AnonymousAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;

/**
 * @Author:Geln Yang
 * @Created:2008-7-28
 */
public class LoginAction extends AnonymousAction {

	private static final String DEFAULT_URL = "index.action";

	private IUserManager userManager;

	private String account;

	private String password;

	public String getOriginalURL() {
		try {
			String url = (String) sessionContext.getResource(ISessionContext.ORIGINAL_URL);
			sessionContext.unbindResource(ISessionContext.ORIGINAL_URL);
			return url;
		} catch (Exception E) {
			return DEFAULT_URL;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		String url = URLUtils.buildOriginalURL(ServletActionContext.getRequest());
		String from = URLUtils.getFileName(url);
		try {
			if (sessionContext.getResource(ISessionContext.ORIGINAL_URL) == null && !from.startsWith("login")) {
				sessionContext.bindResource(ISessionContext.ORIGINAL_URL, url);
			}
		} catch (Exception E) {
		}
		if (account.length() < User.ACCOUNT_MIN_LENGTH || account.length() > User.ACCOUNT_MAX_LENGTH) {
			addFieldError("account", getText("user.account.length.error"));
			return ERROR;
		}
		if (password.length() < User.PASSWORD_MIN_LENGTH || password.length() > User.PASSWORD_MAX_LENGTH) {
			addActionError(getText("login.failed"));
			return ERROR;
		}
		User user = userManager.getUserByAccount(account);
		if (user == null || !user.authorize(password)) {
			addActionError(getText("login.failed"));
			return ERROR;
		}

		ActionContext.getContext().getSession().put(Constants.ACCOUNT, user.getAccount());
		HttpServletResponse response = ServletActionContext.getResponse();
		Cookie c = new Cookie(Constants.COOKIE_NAME, account);
		c.setMaxAge(Constants.COOKIE_MAX_TIME);
		//c.setPath(Constants.COOKIE_PATH);
		response.addCookie(c);
		c = new Cookie(Constants.COOKIE_PASSWORD, password);
		c.setMaxAge(Constants.COOKIE_MAX_TIME);
		//c.setPath(Constants.COOKIE_PATH);
		response.addCookie(c);
		password = "";
		return SUCCESS;
	}

	public String getAccount() {
		return account;
	}

	public String getPassword() {
		return password;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

}
